package com.browsesoft.htmlcomponent;

import java.util.LinkedList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.Element;

import com.browsesoft.EntityManager;
import com.browsesoft.ManagerGroup;
import com.browsesoft.resource.BasicResource;
import com.browsesoft.resource.Resource;

public class NoRootTreeModel implements HTMLTreeModel {
	public NoRootTreeModel() {
	}

	/**
	 * 初始化model
	 * 
	 * @throws Exception
	 */
	public void init(HttpServletRequest request) throws Exception {
	}

	/**
	 * 处理用户请求
	 * 
	 * @param request
	 *            客户端请求
	 */
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	}

	/**
	 * 取得根结点
	 * 
	 * @return 根结点
	 */
	public Object getRootNode() throws Exception {
		// 装载根资源
		EntityManager entitymanager = (EntityManager) ManagerGroup
				.getManagerGroup().getManager("entity");
		return entitymanager.getRoot();
	}

	/**
	 * 根据结点返回子结点
	 * 
	 * @return 子结点
	 */
	public LinkedList getChildNodes(Object node) throws Exception {
		LinkedList temp = new LinkedList();
		if (((Resource) node).getParent() == null) {
			temp = ((BasicResource) node).getSpecialResByType("childbank");
		} else {
			String type = ((Resource) node).getType();
			if (type.equals("childbank")) {
				temp = ((BasicResource) node)
						.getSpecialResByType("productprocess");
				// temp = this.getSpecialResByType( (Resource) node,
				// "productprocess");
			}
		}
		return temp;
	}

	/**
	 * 根据结点返回结点的名称
	 * 
	 * @return 结点的名称
	 */
	public String getName(Object node) throws Exception {
		return ((BasicResource) node).getPath();
	}

	/**
	 * 根据结点返回结点的id
	 * 
	 * @return 结点的名称
	 */
	public String getID(Object node) throws Exception {
		return ((Resource) node).getID();
	}

	/**
	 * 根据id找到结点对象
	 * 
	 * @return 结点对象
	 */
	public Object getObject(String ID) throws Exception {
		EntityManager entitymanager = (EntityManager) ManagerGroup
				.getManagerGroup().getManager("entity");
		return entitymanager.getObject(ID);
	}

	/**
	 * 根据结点返回结点的链接
	 * 
	 * @return 结点的链接
	 */
	public String getLink(Object node) throws Exception {
		return (String) ((Resource) node).getAttributes().get("link");
	}

	/**
	 * 设置模型所对应的组件
	 * 
	 * @param component
	 *            对应的组件
	 */
	public void setComponent(HTMLBasicComponent component) {
	}

	/**
	 * 模型更新页面元素
	 * 
	 * @throws Exception
	 */
	public void updatePageElement(HTMLBasicComponent c) throws Exception {
	}

	public void setContextNode(Element nodeLink, Object modelNode)
			throws Exception {
	}

	/**
	 * 判断是否有子
	 */
	public boolean hasChilds(Object obj) {
		Resource res = (Resource) obj;
		return res.getChildren().hasNext();
	}

	/**
	 * 得到节点对应的图标
	 * 
	 * @param node
	 *            节点
	 * @return 图标
	 */
	public String getIcon(Object node) {
		String icon = (String) ((Resource) node).getAttributes().get("f_icon");
		if (icon == null) {
			icon = "fc_default";
		}
		return icon;
	}
}